<?php
namespace app\app\controller;

use think\Controller;
use think\Session;
class Talk extends Controller
{
    public function index()
    {
        $keyword = input('get.keyword');
        $talk_id = input('get.talk_id');
        if ($keyword != '' ){
            $talk_list = db('talk')->where('commentContent','like','%'.$keyword.'%')->order('commentTime','desc')->paginate(10);
        }elseif ($talk_id != ''){
            $talk_list = db('talk')->where('id',$talk_id)->order('commentTime','desc')->paginate(10);
        }else {
            $talk_list = db('talk')->order('commentTime','desc')->paginate(10);
        }
        $page = $talk_list->render();
        $talk_list = $talk_list->toArray();
        $talk_list = $this->talk_list($talk_list['data']);
        $this->assign('talk_list',$talk_list);
        $this->assign('page',$page);
        $if_login = $this->check_login();
        if ($if_login == false){
            $this->assign('login_status','never');
        }else {
            $never_read_num = $this->never_read();
            $this->assign('never_read_num',$never_read_num);
            $this->assign('user_name',session('user_name','','app'));
            $this->assign('user_id',session('user_id','','app'));
            $this->assign('user_role',session('role','','app'));
            $this->assign('login_status','have');
            $this->assign('logout_url',url('index.php/app/talk/logout'));
            $this->assign('personal_url',url('index.php/app/main/personal_set'));
        }
        $this->assign('active','talk');
        $this->assign('title','论坛');
        $this->view->engine->layout(false);
        return $this->fetch('index');
    }
    
    protected function talk_list($talk_list)
    {
        foreach ($talk_list as $tk => &$tv){
            $tv['commentTime'] = date('Y-m-d H:m:s',$tv['commentTime']);
            $talkhf = db('talkhf')->where('talk_id',$tv['id'])->order('hfTime','desc')->select();
            foreach ($talkhf as $k => &$v){
                $v['hfTime'] = date('Y-m-d H:m:s',$v['hfTime']);
            }
            $tv['talkhf'] = $talkhf;
        }
        return $talk_list;
    }
    
    public function check_login()
    {
        $user_id = Session::has('user_id','app')?Session::get('user_id','app'):false;
        return $user_id;
    }
    
    public function login()
    {
        $result = array('err' => '1','content' => '');
        $user_name = input('post.name');
        $password = input('post.password');
        $password = md5($password);
        $user_id = db('user')->where(['user_name' => $user_name,'password' => $password])->value('id');
        if ($user_id == false){
            $user_info = db('ouser')->where(['user_name' => $user_name,'password' => $password])->find();
            if ($user_info != false){
                $user_id = $user_info['id'];
                $user_warehouse = $user_info['warehouse_id'];
                Session::set('user_warehouse',$user_warehouse,'app');
                $loginlog = model('Loginlog');
                $save_login_log = $loginlog->saveLoginLog($user_info);
                if ($save_login_log == false){
                    $result['content'] = '记录登录失败！';
                    return $result;
                }else {
                    Session::set('role','ouser');
                }
            }else {
                $user_id = false;
            }
            
        }else {
            Session::set('role','admin','app');
        }
        if ($user_id == false){
            $result['content'] = '密码或用户名错误！';
            return $result;
        }
        Session::set('user_id',$user_id,'app');
        Session::set('user_name',$user_name,'app');
        $result['user']['name'] = $user_name;
        $result['user']['id'] = $user_id;
        $result['user']['logout_url'] = url('index.php/app/talk/logout');
        $result['err'] = '0';
        $result['content'] = '登录成功！';
        return $result;
    }
    
    public function logout()
    {
        Session::delete('user_name','app');
        Session::delete('user_id','app');
        Session::delete('user_warehouse','app');
        Session::delete('role','app');
        $result = array('err' => '0','content' => '退出登录成功！');
        return $result;
    }
    
    public function creat_talk()
    {
        $result = array('err' => '1','content' => '');
        $comment = input('post.');
        $talk_model = model('Talk');
        $creat_talk = $talk_model->createTalk($comment);
        if ($creat_talk == false){
            $result['content'] = '发表失败！';
            return $result;
        }
        $result['err'] = '0';
        $result['content'] = '发表成功!';
        $result['talk_id'] = $creat_talk;
        return $result;
    }
    
    public function creat_talkhf()
    {
        $result = array('err' => '1','content' => '');
        $hf_data = input('get.');
        $hf_data['hfNameId'] = Session::get('user_id','app');
        $hf_data['hfName'] = session('user_name','','app');
        $hf_data['hfRole'] = Session::get('role','app');
        if (strstr($hf_data['ohfval'],'回复@') == false){
            $hf_data['hfContent'] = $hf_data['ohfval'];
            $hf_data['atName'] = '';
        }else {
            $arr_tmp = explode('回复@', $hf_data['ohfval']);
            $arr_tmp = explode(':', $arr_tmp[1]);
            $hf_data['hfContent'] = $arr_tmp[1];
            $hf_data['atName'] = $arr_tmp[0];
        } 
        unset($hf_data['ohfval']);
        $talkHf = model('Talkhf');
        $talkhf_id = $talkHf->creatTalkhf($hf_data);
        if ($talkhf_id == false){
            $result['content'] = '回复失败！';
            return $result;
        }
        $result['talkhf_id'] = $talkhf_id;
        $result['err'] = '0';
        $result['content'] = '添加成功！';
        return $result;
    }
    public function save_likes()
    {
        $result = array('err' => '1','content' => '');
        $data = input('post.');
        var_dump($data);
        if (isset($data['talk_id'])){
            $model = model('Talk');
            $data['id'] = $data['talk_id'];
            $save_like = $model->saveLikes($data);
        }elseif (isset($data['talkhf_id'])){
            $model = model('Talkhf');
            $data['id'] = $data['talkhf_id'];
            $save_like = $model->saveLikes($data);
        }
        if ($save_like == false) {
            $result['content'] = '点赞失败！';
            return $result;
        }
        $result['err'] = '0';
        $result['content'] = '点赞成功！';
        return $result;
    }
    
    public function del()
    {
        $result = array('err' => '1','content' => '');
        $del_arr = input('post.');
        if (isset($del_arr['talk_id'])){
            $del = db('talk')->where('id',$del_arr['talk_id'])->delete();
        }else if (isset($del_arr['talkhf_id'])){
            $del = db('talkhf')->where('id',$del_arr['talkhf_id'])->delete();
        }
        if ($del == false){
            $result['content'] = '删除失败！';
            return $result;
        }
        $result['err'] = '0';
        $result['content'] = '删除成功！';
        return $result;
    }
    
    public function never_read_view()
    {
        $user_id = session('user_id','','app');
        $talkhf_list = db('talkhf')->where(['at_id' => $user_id,'read' => '0'])->order('hfTime','desc')->paginate(5);
        $page = $talkhf_list->render();
        $talkhf_list = $talkhf_list->toArray();
        $talkhf_list = $talkhf_list['data'];
        $talkhf_list = $this->transfer_talkhf_time($talkhf_list);
        $this->assign('talkhf',$talkhf_list);
        $talkhf_ids = $this->get_talk_ids($talkhf_list);
        $update_read = db('talkhf')->where('id','in',$talkhf_ids)->update(['read' => '1']);
        $this->assign('page',$page);
        $this->assign('user_name',session('user_name','','app'));
        $this->assign('user_id',$user_id);
        $this->assign('logout_url',url('index.php/app/talk/logout'));
        $this->assign('title','未读信息');
        $this->view->engine->layout(false);
        return $this->fetch('never_read');
    }
    protected function transfer_talkhf_time($talkhf_list)
    {
        foreach ($talkhf_list as $k => &$v){
            $v['hfTime'] = date('y-m-d H:m:s',$v['hfTime']);
        }
        return $talkhf_list;
    }
    protected function get_talk_ids($talkhf_list)
    {
        $talk_ids = array();
        foreach ($talkhf_list as $k => $v){
            array_push($talk_ids, $v['id']);
        }
        return $talk_ids;
        
    }
    
    public function never_read()
    {
        $user_id = Session::get('user_id','app');
        $user_role = Session::get('role','app');
        if ($user_role == 'admin'){
            $never_read_num = db('talkhf')->where(['at_id' => $user_id,'read' => '0','at_role' => 'admin'])->count('*');
        }elseif ($user_role == 'ouser'){
            $never_read_num = db('talkhf')->where(['at_id' => $user_id,'read' => '0','at_role' => 'ouser'])->count('*');
        }
        return $never_read_num;
    }
}